package com.controller;/**
 * @program: SmbmsDemo
 * @description: 供应商控制器
 * @author: Mr.Kang
 * @create: 2020-06-01 12:02
 **/

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.pojo.ProAdd;
import com.pojo.ProModify;
import com.pojo.Provider;
import com.pojo.User;
import com.service.ProviderService;
import com.tools.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description:
 * @author 康康真滴暖
 * @date 2020-06-01 12:02
 */
@Controller
public class ProviderController {
    private Logger log= LogManager.getLogger(ProviderController.class);

    @Resource
    public ProviderService providerService;

    @RequestMapping(value = "addPro")
    public String add(){
        return "provideradd";
    }

    @RequestMapping(value = "selProvide")
    public String selectProvide(HttpServletRequest request, Model model) throws Exception{
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList(null,null);
        model.addAttribute("providerList", providerList);
        return "providerlist";
    }

    @RequestMapping(value = "selProvideBy")
    public String selectProvideByNameCode(@RequestParam(required = false) String queryProName,
                                          @RequestParam(required = false) String queryProCode,
                                          Model model) throws Exception{
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList(queryProName,queryProCode);
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProName", queryProName);
        model.addAttribute("queryProCode", queryProCode);
        return "providerlist";
    }
    @RequestMapping(value = "modifyProvider")
    public String modify(ProModify proModify,HttpServletRequest request, Model model) throws Exception{
       /* String proCode = request.getParameter("proCode");
        String proName = request.getParameter("proName");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proAddress = request.getParameter("proAddress");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");
        String id = request.getParameter("id");*/
        Provider provider = new Provider();
        BeanUtils.copyProperties(proModify,provider);
        provider.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());
        int count = providerService.modify(provider);
        if(count>0){
            return  "redirect:/selProvide";
        }else{
            return "providermodify";
        }
    }
    @RequestMapping(value = "selProviderByIdm/{proid}")
    public String selectProviderByIdModify(@PathVariable("proid") String proid, Model model) throws Exception{
        if (!StringUtils.isNullOrEmpty(proid)) {
            Provider provider = null;
            provider = providerService.getProviderById(proid);
            model.addAttribute("provider", provider);
            return "providermodify";
        }
        return "providermodify";
    }
    @RequestMapping(value = "selProviderByIdv/{proid}")
    public String selectProviderByIdView(@PathVariable("proid") String proid,Model model) throws Exception{
        if (!StringUtils.isNullOrEmpty(proid)) {
            Provider provider = null;
            provider = providerService.getProviderById(proid);
            model.addAttribute("provider", provider);
            return "providerview";
        }
        return "providerview";
    }

    @RequestMapping(value = "addProvider")
    public String addProvider(HttpServletRequest request, HttpServletResponse response, ProAdd proAdd)
            throws Exception{
       /* String proCode = request.getParameter("proCode");
        String proName = request.getParameter("proName");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proAddress = request.getParameter("proAddress");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");*/


        Provider provider = new Provider();
        BeanUtils.copyProperties(proAdd,provider);
        provider.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
       int count= providerService.add(provider);
        if(count>0){
            return "redirect:/selProvide";
        }else{
            return "provideradd";
        }
    }
    @RequestMapping(value = "delProvider")
    @ResponseBody
    public String delProvider(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("proid");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtils.isNullOrEmpty(id)){
            int count = providerService.deleteProviderById(id);
            if(count==0){//删除成功
                resultMap.put("delResult", "true");
            }else{//删除失败
                resultMap.put("delResult", "false");
            }
        }else{
            resultMap.put("delResult", "notexit");
        }
        String a= JSONArray.toJSONString(resultMap);
        return a;
    }
}
